<?php
include dirname(dirname(__FILE__)).'/admin-verify.php';
include dirname(__FILE__).'/functions.php';
?>

<!DOCTYPE html>
<html>
<head>
		<?php include dirname(dirname(dirname(__FILE__))).'/include/common-header.php' ?>
		<script type="text/javascript">
			$(document).ready(function() {
				$("input").attr("maxlength","16");
				$(".required").blur(function () {
					var item = $.trim($(this).val());
					if(item==null||item=='') {
						$(this).next().html("此项不能为空");
						$(this).parent().parent().addClass("has-error");
						$(this).next().fadeIn();
						$("#add-user-btn").attr("disabled","disabled");
					} else {
						$(this).parent().parent().removeClass("has-error");
						$(this).next().hide();
						$("#add-user-btn").removeAttr("disabled");
					}
				});
				$("#username").blur(function (){
					var _username = $.trim($(this).val());
					if(_username==null||_username=='')return;
					if(_username.length<4) {
						$(this).next().html("用户名长度应为4~16位");
						$(this).parent().parent().addClass("has-error");
						$(this).next().fadeIn();
						$("#add-user-btn").attr("disabled","disabled");
						return;
					} else {
						$(this).parent().parent().removeClass("has-error");
						$(this).next().hide();
						$("#add-user-btn").removeAttr("disabled");
					}
					$.ajax({
						dataType:"text",
						url:'ajax/is-username-exists.php',
						data:{username:_username},
						success:function(data) {
							if(data=='yes') {
								$("#username").parent().parent().addClass("has-error");
								$("#username").next().html("此用户名已存在").fadeIn();
								$("#add-user-btn").attr("disabled","disabled");
							} else {
								$("#username").parent().parent().removeClass("has-error");
								$("#username").next().hide();
								$("#add-user-btn").removeAttr("disabled");
							}
						},
					});
				});
				$("#password").blur(function() {
					var _password = $.trim($(this).val());
					if(_password==null||_password=='')return;
					if(_password.length<4) {
						$("#password").parent().parent().addClass("has-error");
						$("#password").next().html("密码长度应为4~16位").fadeIn();
						$("#add-user-btn").attr("disabled","disabled");
					} else {
						$("#password").parent().parent().removeClass("has-error");
						$("#password").next().hide();
						$("#add-user-btn").removeAttr("disabled");
					}
				});
				$("#add-user-btn").click(function() {
					if($("#username").val()==''||$("#password").val()==''
						||$("#user_role").val()=='') {
						alert("请完成必填项!");
						$("#add-user-btn").attr("disabled","disabled");
						return false;
					} 
					return true;
				});
			});
			
		</script>
		<link rel="stylesheet" type="text/css" href="../css/style.css">
</head>
<body>
	<div class="col-md-12">
		<div>
			<h3>添加员工</h3>
		</div>
		<div>
			<form class="form-horizontal" role="form" method="post" action="add-user.php">
			  <div class="form-group">
			    <label for="username" class="col-sm-2 control-label">用户名</label>
			    <div class="col-sm-10">
			      <input type="text" name="username" class="form-control normal-input required" id="username" placeholder="用户名">
			   	  <p class="help-block" style="display:none"></p>
			    </div>
			  </div>
			  <div class="form-group">
			    <label for="password" class="col-sm-2 control-label">密码</label>
			    <div class="col-sm-10">
			      <input type="password" name="password" class="form-control normal-input required" id="password" placeholder="密码">
			    	<p class="help-block" style="display:none"></p>
			    </div>
			  </div>
			  <div class="form-group">
			    <label for="student_id" class="col-sm-2 control-label">学号</label>
			    <div class="col-sm-10">
			      <input type="text" name="student_id" class="form-control normal-input" id="student_id" placeholder="学号">
			    </div>
			  </div>
			  <div class="form-group">
			    <label for="department" class="col-sm-2 control-label">学院</label>
			    <div class="col-sm-10">
			      <input type="text" name="department" class="form-control normal-input" id="department" placeholder="学院">
			    </div>
			  </div>
			  <div class="form-group">
			    <label for="grade" class="col-sm-2 control-label">年级</label>
			    <div class="col-sm-10">
			      <input type="text" name="grade" class="form-control normal-input" id="grade" placeholder="年级">
			    </div>
			  </div>
			  <div class="form-group">
			    <label for="user_real_name" class="col-sm-2 control-label">姓名</label>
			    <div class="col-sm-10">
			      <input type="text" name="user_real_name" class="form-control normal-input" id="user_real_name" placeholder="姓名">
			    </div>
			  </div>
			  <div class="form-group">
			    <label for="sex" class="col-sm-2 control-label">性别</label>
			    <div class="col-sm-10">
			      <input type="text" name="sex" class="form-control normal-input" id="sex" placeholder="性别">
			    </div>
			  </div>
			  <div class="form-group">
			    <label for="phone" class="col-sm-2 control-label">联系方式</label>
			    <div class="col-sm-10">
			      <input type="text" name="phone" class="form-control normal-input" id="phone" placeholder="手机或电话">
			    </div>
			  </div>
			  
			  <div class="form-group">
			    <label for="user_role" class="col-sm-2 control-label">用户类型</label>
			    <div class="col-sm-10">
			      <select class="form-control normal-input required" id="user_role" name="user_role">
			      <option value=""></option>
			      <option value="user">普通用户</option>
			      <?php 
			      		if($_SESSION['user_role']=='super_admin')
			      			echo '<option value="admin">管理员</option>';
			      ?>
				  </select>
				  <p class="help-block" style="display:none"></p>
			    </div>
			  </div>
			  <div class="form-group">
			  <label for="is_user_on_duty" class="col-sm-2 control-label" >是否需要值班</label>
			    <div class="col-sm-10">
			      <div class="checkbox">
			        <label>
			          <input type="checkbox" id="is_user_on_duty" name="is_user_on_duty" value="1">需要值班
			        </label>
			      </div>
			    </div>
			  </div>
			  <div class="form-group">
			    <div class="col-sm-offset-2 col-sm-10">
			      <button type="submit" class="btn btn-primary" id="add-user-btn">添加用户</button>
			    </div>
			  </div>
			</form>
		</div>
	</div>
</body>